﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NewBee.Core.Data;
using NewBee.Core.Data.Model;

namespace NewBee.Core.DataService
{
    public class BaseDataService
    {
        /// <summary>
        /// 
        /// </summary>
        public virtual DbHelper _DbHelper
        {
            get
            {
                return new DbHelper(DataConnection.ConnectionString, DataConnection.QueryConnectionString,
                    DataConnection.DriverType);
            }
        }

        public virtual string TableName { set; get; }

        ///// <summary>
        ///// 返回查询列表
        ///// </summary>
        ///// <param name="where"></param>
        ///// <returns></returns>
        //public List<dynamic> Query(QueryParam queryParam)
        //{
        //    if (string.IsNullOrEmpty(queryParam.select))
        //        queryParam.select = " * ";
        //    string _where = string.Empty;
        //    if (!string.IsNullOrEmpty(queryParam.where))
        //        _where += " Where " + queryParam.where;

        //    string _orderBy = string.Empty;
        //    if (!string.IsNullOrEmpty(queryParam.sort))
        //        _orderBy += " Order By " + queryParam.sort + " " + queryParam.order;

        //    string _top = string.Empty;
        //    if (queryParam.top >= 0)
        //        _top = "Top " + queryParam.top + " ";

        //    return this.DB.QueryDao.Sql(@"Select " + _top + " " + queryParam.select + " From " + TableName + " " + _where + " " + _orderBy)
        //        .QueryMany<dynamic>();
        //}

        #region 方法
        /// <summary>
        ///  执行增删改sql语句
        /// </summary>
        /// <param name="sqlHelper">SQL帮助类</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public int Execute(SQLHelper sqlHelper, object param)
        {
            return _DbHelper.Execute(sqlHelper.ToString(), param);
        }

        /// <summary>
        /// 返回查询条数
        /// </summary>
        /// <param name="where"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public int Count(string where, object param)
        {
            SQLHelper sql = new SQLHelper(TableName).Columns(new List<string> { "COUNT(*)" }).Where(where);

            return QueryFirst<int>(sql.ToString(), param);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="_PageParam"></param>
        /// <returns></returns>
        public PageResult QueryPage(PageParam _PageParam)
        {
            PageResult _PageResult = new PageResult();
            _PageResult.total = Count(_PageParam.whereSql, _PageParam.param);

            SQLHelper sql = new SQLHelper(TableName)
                .Page(_PageParam.page, _PageParam.rows)
                .Columns(_PageParam.selectColumn.Split(',').ToList())
                .Where(_PageParam.whereSql);

            foreach (var order in _PageParam.orderParam)
            {
                sql.Order(order.Key, order.Value);
            }

            _PageResult.rows = Query<dynamic>(sql.ToString(), _PageParam.param);

            return _PageResult;
        }

        /// <summary>
        /// 执行SQL，返回第一行第一个元素的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public T QueryFirst<T>(string sql, object param)
        {
            return _DbHelper.QueryFirst<T>(sql, param);
        }

        /// <summary>
        /// 执行SQL，返回数据实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<T> Query<T>(string sql, object param)
        {
            return _DbHelper.Query<T>(sql, param);
        }
        #endregion
    }
}
